/*
 * Copyright (C) 2011 Nanohex Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.nanohexcorp.kudocode.snapqr;

import java.util.ArrayList;

import com.nanohexcorp.kudocode.snapqr.db.SimpleDB;
import com.nanohexcorp.kudocode.snapqr.object.QRLinksObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

public class SnapQRView extends Activity {
private WebView m_WebView;
private ArrayList<QRLinksObject> mARQRLinksObject;
private int mCurrentIndex;

	/** Called when the activity is first created. */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.getWindow().requestFeature(Window.FEATURE_PROGRESS);

		setContentView(R.layout.snapqr_view);
		getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);

		mARQRLinksObject = SimpleDB.QRLinksTable.getAll();

		Bundle extras = getIntent().getExtras();
		findCurrentIndex(extras.getInt("id"));

        setupButtons();
        setupWebView();
        loadWebView();
	}

    private void returnToMainActivity()
    {
    	startActivityForResult(new Intent(getApplicationContext(), SnapQR.class), 0);
    }

    private void setupWebView()
    {
    	m_WebView = (WebView) findViewById(R.id.webview);
		WebSettings webSettings = m_WebView.getSettings();
		webSettings.setSavePassword(false);
        webSettings.setSaveFormData(false);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setSupportZoom(false);

        final Activity activity = this;

        final ProgressDialog progressDialog = new ProgressDialog(activity);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setCancelable(false);

        m_WebView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return false;
           }
        });

        m_WebView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                progressDialog.show();
                progressDialog.setProgress(0);
                activity.setProgress(progress * 1000);
                progressDialog.incrementProgressBy(progress);
                if(progress == 100 && progressDialog.isShowing())
                    progressDialog.dismiss();
            }
        });
    }

    private void setupButtons()
    {
        Button btnNext = (Button) findViewById(R.id.bar_next);
        Button btnPrevious = (Button) findViewById(R.id.bar_previous);
        Button btnReturn = (Button) findViewById(R.id.bar_return);

        btnReturn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	returnToMainActivity();
            }
        });


        btnNext.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	nextCurrentIndex();
            	loadWebView();
            }
        });

        btnPrevious.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	previousCurrentIndex();
            	loadWebView();
            }
        });
    }

	private void loadWebView()
	{
		QRLinksObject obj = getObjAtIndex();
		m_WebView.loadUrl(obj.getUrl());
	}

	private void findCurrentIndex(int id)
	{
		mCurrentIndex = 0;
        for(QRLinksObject tmpQRLinksObject : mARQRLinksObject) {
        	if(tmpQRLinksObject.getID() == id) break;
        	mCurrentIndex++;
        }
		if(mCurrentIndex > mARQRLinksObject.size())
			mCurrentIndex = mARQRLinksObject.size() - 1;
	}

	private void nextCurrentIndex()
	{
		if((mCurrentIndex+1) < mARQRLinksObject.size()) mCurrentIndex++;
		else mCurrentIndex = 0;
	}

	private void previousCurrentIndex()
	{
		if(mCurrentIndex > 0) mCurrentIndex--;
		else mCurrentIndex = mARQRLinksObject.size() - 1;
	}

	private QRLinksObject getObjAtIndex()
	{
		return mARQRLinksObject.get(mCurrentIndex);
	}
}
